// pages/goods/goods.js
Page({
  data: {
    // 跳转新建页面
    navigateToAdd: function() {
      wx.navigateTo({
        url: '/pages/goods-add/goods-add'
      });
    },

    goodsList: [],
    loading: true
  },

  onLoad() {
    this.loadGoodsList();
  },

  loadGoodsList() {
    this.setData({ loading: true });
    http.request({
      url: apiConfig.getGoodsList,
      method: 'GET',
      data: {
        page: 1,
        per_page: 10
      }
    }).then(res => {
      if (res.status != 200) return Promise.reject();
      this.setData({
        goodsList: res.data.data.map(item => ({
          ...item,
          profit: (item.sales_price - item.cost_price).toFixed(2),
          manufacturer: item.manufacturer?.manufacturer_name || '未知厂家'
        })),
        loading: false
      });
    }).catch(() => {
      wx.showToast({ title: '加载失败', icon: 'none' });
      this.setData({ loading: false });
    });
  },

  navigateToAdd() {
    wx.navigateTo({ url: '/pages/goods-add/goods-add' });
  },

  handleEdit(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({ url: `/pages/goods-edit/goods-edit?id=${id}` });
  },

  handleDelete(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认删除',
      content: '确定要删除该产品吗？',
      success: res => {
        if (res.confirm) {
          // 调用删除接口
        }
      }
    });
  }
});
import http from '../../utils/http';
import apiConfig from '../../utils/apiConfig';